import { useSceneStore } from '@/store/scene';
const sceneStore = useSceneStore();

export function updateLayerAttribute(fieldName: string, valid: boolean) {
  const { cimInstance } = window;
  sceneStore.editError = !valid;
  if (sceneStore.editError || !sceneStore.selLayer) return;
  // 编辑图层
  if (['gltfmodel', 'entity'].includes(sceneStore.selLayer.type)) {
    let translation = [0, 0, 0];
    let rotation = [0, 0, 0];
    if (sceneStore.selLayer.attr.translation) {
      translation = sceneStore.selLayer.attr.translation.map((v: any) => Number(v));
    }
    if (sceneStore.selLayer.attr.rotation) {
      rotation = sceneStore.selLayer.attr.rotation.map((v: any) => Number(v));
    }
    const param: any = {
      pose: {
        position: sceneStore.selLayer.attr.position?.map((v: any) => Number(v)),
        translation,
        rotation,
        scale: Number(sceneStore.selLayer.attr.scale),
      },
    };
    if (sceneStore.selLayer.attr.color) {
      param.color = sceneStore.selLayer.attr.color;
    }
    console.log('-----更新图层------', param);
    cimInstance.api.updateLayerGraphics(sceneStore.selLayer.layerName, param);
    const rotation_model = {
      x: Number(rotation[0]),
      y: Number(rotation[1]),
      z: Number(rotation[2]),
    };
    coverData(param);
    const position = cimInstance.api.getTransformedPosition(param.pose);
    const position_model = {
      x: Number(position[0]),
      y: Number(position[1]),
      z: Number(position[2]),
    };
    cimInstance.api.updateEffectModel({
      position: position_model,
      rotation: rotation_model,
      scale: Number(sceneStore.selLayer.attr.scale),
    });
    cimInstance.api.refreshOrbitController();
    return;
  }
  if (['drawlabel'].includes(sceneStore.selLayer.type)) {
    // const config = {
    //   entityId: sceneStore.selLayer.attr.entityId,
    //   image: {
    //     imageUrl: sceneStore.selLayer.attr.image,
    //     width: Number(sceneStore.selLayer.attr.width),
    //     height: Number(sceneStore.selLayer.attr.height),
    //     scale: 2, // 标签闪烁
    //     isBlink: sceneStore.selLayer.attr.isBlink, // 标签闪烁
    //     heightReference: sceneStore.selLayer.attr.heightReference ? 'CLAMP_TO_GROUND' : 'NONE', // 标签贴地
    //     scaleByDistanceArray: sceneStore.selLayer.attr.scaleByDistanceArray.map((v: any) =>
    //       Number(v)
    //     ),
    //   },
    //   coordinates: sceneStore.selLayer.attr.position.map((v: any) => Number(v)),
    // };
    // cimInstance.api.updateLayerGraphics(sceneStore.selLayer.layerName, config);

    return;
  }
  if (['fake_particleSystem'].includes(sceneStore.selLayer.type)) {
    // const config = {
    //   ...sceneStore.selLayer.attr.config,
    //   longitude: Number(sceneStore.selLayer.attr.position[0]),
    //   latitude: Number(sceneStore.selLayer.attr.position[1]),
    //   height: Number(sceneStore.selLayer.attr.position[2]),
    //   semiMajorAxis: Number(sceneStore.selLayer.attr.semiMajorAxis),
    //   semiMinorAxis: Number(sceneStore.selLayer.attr.semiMinorAxis),
    //   speed: Number(sceneStore.selLayer.attr.speed),
    //   circleCount: Number(sceneStore.selLayer.attr.circleCount),
    // };

    // cimInstance.api.updateLayerGraphics(sceneStore.selLayer.layerName, {
    //   pose: {
    //     position: sceneStore.selLayer.attr.position.map((v: any) => Number(v)),
    //     translation: [0, 0, 0],
    //     rotation: sceneStore.selLayer.attr.rotation.map((v: any) => Number(v)),
    //     scale: Number(sceneStore.selLayer.attr.scale),
    //   },
    //   color: sceneStore.selLayer.attr.color || '',
    // });
    // const position = {
    //   x: Number(sceneStore.selLayer.attr.position[0]),
    //   y: Number(sceneStore.selLayer.attr.position[1]),
    //   z: Number(sceneStore.selLayer.attr.position[2]),
    // };
    // const rotation = {
    //   x: Number(sceneStore.selLayer.attr.rotation[0]),
    //   y: Number(sceneStore.selLayer.attr.rotation[1]),
    //   z: Number(sceneStore.selLayer.attr.rotation[2]),
    // };
    // cimInstance.api.updateEffectModel({
    //   position,
    //   rotation,
    //   scale: Number(sceneStore.selLayer.attr.scale),
    // });
    // cimInstance.api.refreshOrbitController();
    // cimInstance.api.replaceParticleSystem(sceneStore.selLayer.json.components[0].name, config);
    return;
  }

  if (['tileset'].includes(sceneStore.selLayer.type)) {
    let translation = [0, 0, 0];
    let rotation = [0, 0, 0];
    if (sceneStore.selLayer.attr.translation) {
      translation = sceneStore.selLayer.attr.translation.map((v: any) => Number(v));
    }
    if (sceneStore.selLayer.attr.rotation) {
      rotation = sceneStore.selLayer.attr.rotation.map((v: any) => Number(v));
    }
    const param: any = {
      pose: {
        translation,
        rotation,
        scale: Number(sceneStore.selLayer.attr.scale),
      },
    };
    if (sceneStore.selLayer.attr.color) {
      param.color = sceneStore.selLayer.attr.color;
    }
    coverData(param);
    param.material = sceneStore.selLayer.attr.material;
    console.log('-----更新图层------', param);
    cimInstance.api.updateLayerGraphics(sceneStore.selLayer.layerName, param);
  }
  if (['point', 'polyline', 'polygon'].includes(sceneStore.selLayer.type)) {
    cimInstance.api.updateLayerGraphics(sceneStore.selLayer.layerName, sceneStore.selLayer.attr);
  }
  if (['bimModel'].includes(sceneStore.selLayer.type)) {
    const position = [
      Number(sceneStore.selLayer.attr.position[0]),
      Number(sceneStore.selLayer.attr.position[1]),
      Number(sceneStore.selLayer.attr.position[2]),
    ];
    let translation = [0, 0, 0];
    let rotation = [0, 0, 0];
    if (sceneStore.selLayer.attr.translation && sceneStore.selLayer.attr.translation) {
      translation = sceneStore.selLayer.attr.translation.map((v: any) => Number(v));
    }
    if (sceneStore.selLayer.attr.rotation && sceneStore.selLayer.attr.translation) {
      rotation = sceneStore.selLayer.attr.rotation.map((v: any) => Number(v));
    }
    const param = {
      pose: {
        position,
        translation,
        rotation,
        scale: Number(sceneStore.selLayer.attr.scale),
      },
      color: sceneStore.selLayer.attr.color || '',
    };
    coverData(param);
    console.log('xxxxx', param);
    cimInstance.api.updateLayerGraphics(sceneStore.selLayer.layerName, param);
    cimInstance.api.refreshOrbitController(fieldName);
  }
  if (['image', 'imageLayer'].includes(sceneStore.selLayer.type)) {
    if (!sceneStore.selLayer.attr) return;
    cimInstance.api.setLayerProperty(sceneStore.selLayer.layerName, sceneStore.selLayer.attr);
  }
}

// 清空对象里没有值的key
function coverData(data: any) {
  for (const key in data) {
    if (!data[key]) {
      delete data[key];
    } else {
      if (Object.prototype.toString.call(data[key]) === '[object Object]') {
        coverData(data[key]);
      }
    }
  }
}
